package com.zp.business.impl.register;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.register.RegistrationItem;
import com.zp.business.entity.register.bo.RegistrationItemBo;
import com.zp.business.entity.register.vo.RegistrationItemVo;
import com.zp.business.mapper.register.RegistrationItemMapper;
import com.zp.business.service.register.IRegistrationItemService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 挂号明细Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-12-23
 */
@RequiredArgsConstructor
@Service
public class RegistrationItemServiceImpl implements IRegistrationItemService {

    private final RegistrationItemMapper baseMapper;

    /**
     * 查询挂号明细
     */
    @Override
    public RegistrationItemVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询挂号明细列表(分页)
     */
    @Override
    public TableDataInfo<RegistrationItemVo> queryPageList (RegistrationItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RegistrationItem> lqw = buildQueryWrapper(bo);
        Page<RegistrationItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询挂号明细列表(不分页)
     */
    @Override
    public List<RegistrationItemVo> queryList (RegistrationItemBo bo) {
        LambdaQueryWrapper<RegistrationItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RegistrationItem> buildQueryWrapper (RegistrationItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RegistrationItem> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClinicId()), RegistrationItem::getClinicId, bo.getClinicId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemClass()), RegistrationItem::getItemClass, bo.getItemClass());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), RegistrationItem::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCode()), RegistrationItem::getItemCode, bo.getItemCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceId()), RegistrationItem::getPriceId, bo.getPriceId());
        lqw.eq(bo.getNum() != null, RegistrationItem::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnRcpt()), RegistrationItem::getClassOnRcpt, bo.getClassOnRcpt());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnReckoning()), RegistrationItem::getClassOnReckoning, bo.getClassOnReckoning());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjCode()), RegistrationItem::getSubjCode, bo.getSubjCode());
        lqw.eq(bo.getCosts() != null, RegistrationItem::getCosts, bo.getCosts());
        lqw.eq(bo.getCharges() != null, RegistrationItem::getCharges, bo.getCharges());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), RegistrationItem::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), RegistrationItem::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增挂号明细
     */
    @Override
    public Boolean insertByBo (RegistrationItemBo bo) {
        RegistrationItem add = BeanUtil.toBean(bo, RegistrationItem.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改挂号明细
     */
    @Override
    public Boolean updateByBo (RegistrationItemBo bo) {
        RegistrationItem update = BeanUtil.toBean(bo, RegistrationItem.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (RegistrationItem entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除挂号明细
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean insetBatch (List<RegistrationItem> list) {
        return baseMapper.insertBatch(list);
    }
}
